package com.qiwx.pigeon.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.data.params.FertilizeParam;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description
 * @Author qiwx
 * @Date 2020-05-23 12:01
 **/
public class AddLayEggOptionView extends LinearLayout {

    @BindView(R.id.rb_egg2)
    RadioButton rbEgg2;
    @BindView(R.id.rb_egg1)
    RadioButton rbEgg1;
    @BindView(R.id.rb_wudan)
    RadioButton rbWudan;
    @BindView(R.id.rb_egg_cpd)
    RadioButton rbEggCpd;
    @BindView(R.id.rb_bfd)
    RadioButton rbBfd;
    @BindView(R.id.group_check_egg_1)
    RadioGroup groupCheckEgg1;
    @BindView(R.id.rb_shouj2)
    RadioButton rbShouj2;
    @BindView(R.id.rb_sij2)
    RadioButton rbSij2;
    @BindView(R.id.rb_wuj2)
    RadioButton rbWuj2;
    @BindView(R.id.rb_shou2wuj1)
    RadioButton rbShou2wuj1;
    @BindView(R.id.rb_shou2sij1)
    RadioButton rbShou2sij1;
    @BindView(R.id.rb_wu2sij1)
    RadioButton rbWu2sij1;
    @BindView(R.id.group_fertilize2)
    GridRadioGroup groupFertilize2;
    @BindView(R.id.rb_shouj1)
    RadioButton rbShouj1;
    @BindView(R.id.rb_wujing1)
    RadioButton rbWujing1;
    @BindView(R.id.rb_sijing1)
    RadioButton rbSijing1;
    @BindView(R.id.group_fertilize1)
    RadioGroup groupFertilize1;
    @BindView(R.id.cb_empty)
    CheckBox cbEmpty;
    int currentCheckId = 0;//当前选择的受精类型
    @BindView(R.id.rb_number0)
    RadioButton rbNumber0;
    @BindView(R.id.rb_number1)
    RadioButton rbNumber1;
    @BindView(R.id.rb_number2)
    RadioButton rbNumber2;
    @BindView(R.id.rb_number3)
    RadioButton rbNumber3;
    @BindView(R.id.rg_number)
    RadioGroup rgNumber;
    @BindView(R.id.cb_open_sj)
    CheckBox cbOpenSj;
    @BindView(R.id.lin_shoujing)
    LinearLayout linShoujing;

//    @BindView(R.id.cb_jd)//不需要捡蛋模式，捡蛋其实就是捡蛋后设置成空窝
//    CheckBox cbJd;


    public AddLayEggOptionView(Context context) {
        this(context, null);
    }

    public AddLayEggOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddLayEggOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initEvent();

    }
//    public AddLayEggOptionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        initView(context);
//        initEvent();
//    }

    public void initView(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.add_lay_egg_option, null);
        ButterKnife.bind(this, view);
        rbEggCpd.setVisibility(View.GONE);
        rbBfd.setVisibility(View.GONE);
        rgNumber.setVisibility(GONE);
        linShoujing.setVisibility(GONE);
        addView(view);
    }

    private void initEvent() {
        //如果是捡蛋模式，
//        cbJd.setOnCheckedChangeListener((btn, ischeck) -> {
//            if (ischeck) {
//
//            } else {
//
//            }
//        });
        cbOpenSj.setOnCheckedChangeListener((group, isCheck) -> {
            if (isCheck) {
                linShoujing.setVisibility(VISIBLE);
            } else {
                linShoujing.setVisibility(GONE);
            }

        });
        groupFertilize2.setOnCheckedChangeListener((group, checkId) -> {
            currentCheckId = checkId;
        });
        groupFertilize1.setOnCheckedChangeListener((group, checkId) -> {
            currentCheckId = checkId;
        });
        rbWudan.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.GONE);
            }

        });
        rbEgg1.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbShouj1.setChecked(true);
                groupFertilize2.setVisibility(View.GONE);
                groupFertilize1.setVisibility(View.VISIBLE);

            }
        });
        rbEgg2.setOnCheckedChangeListener((btn, isCheck) -> {
            if (isCheck) {
                rbShouj2.setChecked(true);
                groupFertilize2.setVisibility(View.VISIBLE);
                groupFertilize1.setVisibility(View.GONE);
            }
        });
        groupFertilize1.setVisibility(View.GONE);
        rbWudan.setVisibility(View.GONE);

    }

    //是否显示受精部分选项
    public void setShouJingShow(boolean isShow) {
        groupCheckEgg1.setVisibility(isShow ? VISIBLE : GONE);
        groupFertilize2.setVisibility(isShow ? VISIBLE : GONE);
        groupFertilize1.setVisibility(isShow ? VISIBLE : GONE);

    }

    //重置选项
    public void resetOptions() {
        cbEmpty.setChecked(false);
//        cbJd.setChecked(false);
        rbNumber2.setChecked(true);
        if (rbEgg2.isChecked()) {
            rbShouj2.setChecked(true);
        } else {
            rbEgg2.setChecked(true);

        }

    }

    //正常下蛋日期
    public FertilizeParam getParam() {
        FertilizeParam param = new FertilizeParam();
        int loseAmount = 0;//损失数量 默认是2
        if (rbNumber0.isChecked()) {
            loseAmount = 0;
        } else if (rbNumber1.isChecked()) {
            loseAmount = 1;
        } else if (rbNumber2.isChecked()) {
            loseAmount = 2;
        } else if (rbNumber3.isChecked()) {
            loseAmount = 3;
        }
        if (rbBfd.isChecked()) {
            param.setBfd(loseAmount);
        } else if (rbEggCpd.isChecked()) {
            param.setBreaken(loseAmount);
        } else if (rbWudan.isChecked()) {
            param.setWudan(loseAmount);
        }
        param.setAmount(rbEgg2.isChecked() ? 2 : 1);
        param.setEmptyCage(cbEmpty.isChecked());
//        param.setJianDan(cbJd.isChecked());
        int eggStatus = 0;
        if (cbOpenSj.isChecked()) {
            eggStatus = DBSqlUtil.getFerilizeStatusByTpye(currentCheckId);
            param.setFudan(true);
        } else {
            param.setFudan(false);
        }
        param.setEgg_status(eggStatus);
        param.setIsLimit(0);
        return param;

    }

    //补录下蛋操作获取参数
    public FertilizeParam getBLParam() {
        FertilizeParam param = new FertilizeParam();
        if (rbEgg1.isChecked() || rbEgg2.isChecked()) {
            if (cbEmpty.isChecked()) {
                param.setFudan(false);
            } else {
                param.setFudan(true);
            }

        } else {
            param.setFudan(false);
        }
        int bad2breaken = 0;
        if (rbNumber0.isChecked()) {
            bad2breaken = 0;
        } else if (rbNumber1.isChecked()) {
            bad2breaken = 1;
        } else if (rbNumber2.isChecked()) {
            bad2breaken = 2;
        } else if (rbNumber3.isChecked()) {
            bad2breaken = 3;
        }
        if (rbBfd.isChecked()) {
            param.setBad(bad2breaken);
        } else if (rbEggCpd.isChecked()) {
            param.setBreaken(bad2breaken);
        }
        param.setEmptyCage(cbEmpty.isChecked());
//        param.setJianDan(cbJd.isChecked());
        param.setIsLimit(0);
        int eggStatus = DBSqlUtil.getFerilizeStatusByTpye(currentCheckId);
        param.setEgg_status(eggStatus);
        return param;
    }
}
